package com.yxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yxh.dao.HideMapper;
import com.yxh.dao.ViciousMapper;
import com.yxh.pojo.Hide;
import com.yxh.service.FlowManageService;
import com.yxh.service.HideService;
import com.yxh.util.SnowflakeIdWorker;
import com.yxh.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by asus on 2018/6/28.
 */

@Service
@Transactional
public class HideServiceImpl implements HideService {


    @Autowired
    private HideMapper hideMapper;
    @Autowired
    private FlowManageService flowManageService;

    @Autowired
    private ViciousMapper viciousMapper;

    @Override
    public int save(Hide hide) {
        try {
            String imgs=hide.getImgId();
            if(StringUtil.isNotEmpty(imgs)){
                String id= SnowflakeIdWorker.creatId();
                flowManageService.insertImgs(imgs,id);
                hide.setImgId(id);
            }
            return hideMapper.insertSelective(hide);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public Map<String, Object> getById(String hideId) {

        Map<String, Object> m=hideMapper.selectByPrimaryKey(hideId);
        if(m!=null&&m.get("auditingId")!=null)
            m.put("auditingName", flowManageService.nameBack(m.get("auditingId").toString()));


        return m;
    }

    @Override
    public void updata(Hide hide) {

        hideMapper.updateByPrimaryKeySelective(hide);
    }

    @Override
    public Map<String, Object> getNewById(String id) {

        Map<String, Object> m=hideMapper.selectByPrimaryKey(id);
        if(m!=null&&m.get("imgId")!=null){
            m.put("imgs",viciousMapper.getListImgs((String) m.get("imgId")));
        }
        if(m!=null&&m.get("auditingId")!=null)
            m.put("auditingName", flowManageService.nameBack(m.get("auditingId").toString()));


        return m;
    }

    @Override
    public PageInfo getInformation(Hide hide, Integer limit, Integer offset) {
        PageHelper.startPage(limit, offset);
        List<Map<String,Object>> menuList =hideMapper.selectByHide(hide);
        PageInfo pageInfo = new PageInfo(menuList);
        pageInfo.setList(menuList);
        return pageInfo;
    }
}
